package com.lse_api.utils.encryption;


import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;


public class MD5Encryption {

	 //ʮ�����������ֵ��ַ���ӳ������ 
    private final static String[] hexDigits = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; 
	

  //**********************MD5加密1*******************************//
	public static String encodeBymd5(String user_id , String password ) {

		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
		Integer math = (int)(100 + Math.random() * 999999) ;
		 /**���ַ�������MD5����*/
		String token = user_id + password + format.format(new Date()) + math.toString() ;
	        if (token != null) {
	            try { 
	                //��������ָ���㷨���Ƶ���ϢժҪ 
	                MessageDigest md5 = MessageDigest.getInstance("MD5"); 
	                //ʹ��ָ�����ֽ������ժҪ���������£�Ȼ�����ժҪ���� 
	                byte[] results = md5.digest(token.getBytes()); 
	                //���õ����ֽ��������ַ�������  byteArrayToHexString
	                String result = byteArrayToHexString(results); 
	                return result; 
	            } catch (Exception e) { 
	                e.printStackTrace(); 
	            } 
	        } 
	        return null; 
	}
	
	/** 
	    * �ֻ��ֽ�����Ϊʮ�������ַ��� 
	    * @param b �ֽ����� 
	    * @return ʮ�������ַ��� 
	    */ 
	    private static String byteArrayToHexString(byte[] b){ 
	        StringBuffer resultSb = new StringBuffer(); 
	        for(int i=0;i<b.length;i++){ 
	            resultSb.append(byteToHexString(b[i])); 
	        } 
	        return resultSb.toString(); 
	    }
	    
	  //��һ���ֽ�ת����ʮ��������ʽ���ַ��� 
	    private static String byteToHexString(byte b){ 
	        int n = b; 
	        if(n<0) 
	        n=256+n; 
	        int d1 = n/16; 
	        int d2 = n%16; 
	        return hexDigits[d1] + hexDigits[d2]; 
	    }
	    
	    
	    /**
	     * ****MD5****
	     * @param args
	     */
	    public static void main(String[] args) {
			for (int i = 0; i < 10; i++) {
				System.out.println(encodeBymd5("1" , "123"));
			}
		}
	
}
